package cn.hzking.gjmall.activitys.second;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fingdo.statelayout.StateLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.hzking.gjmall.R;
import cn.hzking.gjmall.adpters.category.CateGoryGoodListAdapter;
import cn.hzking.gjmall.base.ColorStatuBarActivity;
import cn.hzking.gjmall.bean.CategoryListBean;
import cn.hzking.gjmall.net.RetrofitFactory;
import cn.hzking.gjmall.net.base.GjCustomStateViewObserver;
import cn.hzking.gjmall.utils.SwipeRefreshLayoutUtils;

/**
 * 分类列表二级条目点击进入的页面
 */
public class CateGorySecondGoodListActivity extends ColorStatuBarActivity {

    @Bind(R.id.back_search)
    RelativeLayout backSearch;
    @Bind(R.id.search_et)
    TextView searchEt;
    @Bind(R.id.category_goodlist_ll)
    LinearLayout categoryGoodlistLl;
    @Bind(R.id.recyclerview)
    RecyclerView recyclerview;
    @Bind(R.id.refrsh)
    SwipeRefreshLayout refrsh;
    @Bind(R.id.statlayout)
    StateLayout statlayout;
    private ArrayList<String> secondIds;//二级条目的集合
    private ArrayList<String> itemcontents;//滑动条目内容
    private View[] views;
    private TextView[] toolsTextViews;
    private int currentPage = 1;
    private CateGoryGoodListAdapter cateGoryGoodListAdapter;
    private int flag = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_cate_gory_second_good_list);
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        secondIds = intent.getStringArrayListExtra("secondId");
        itemcontents = intent.getStringArrayListExtra("itemcontent");
        int index = intent.getIntExtra("index", -1);
        views = new View[itemcontents.size()];
        toolsTextViews = new TextView[itemcontents.size()];
        for (int i = 0; i < itemcontents.size(); i++) {
            View inflate = View.inflate(this, R.layout.item_categorylist_view, null);
            inflate.setTag(i);
            TextView textView = inflate.findViewById(R.id.item_textview);
            views[i] = inflate;
            toolsTextViews[i] = textView;
            if (i == index) {
                flag = i;
                textView.setSelected(true);
            }
            textView.setText(itemcontents.get(i));
            categoryGoodlistLl.addView(inflate);
            inflate.setOnClickListener(listener);
        }
        SwipeRefreshLayoutUtils.initRefreshLayout(refrsh);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        recyclerview.setLayoutManager(gridLayoutManager);
        cateGoryGoodListAdapter = new CateGoryGoodListAdapter(R.layout.item_goodlist, null);
        recyclerview.setAdapter(cateGoryGoodListAdapter);
        getNetData(index, true, false, true, true, null, null, null);
        initListener();
    }

    private void initListener() {
        //返回按钮
        backSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //刷新
        refrsh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                getNetData(flag, false, true, false, true, null, null, null);

            }
        });

        //上拉加载更多
        cateGoryGoodListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                getNetData(flag, false, false, false, true, null, null, null);
            }
        });
    }


    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            currentPage = 1;
            int tag = (int) v.getTag();//在线性布局中的位置
            flag = tag;
            //点击变化按钮状态
            checkSecect(tag);
            getNetData(flag, true, false, true, true, null, null, null);


        }
    };

    private void checkSecect(int tag) {
        for (int i = 0; i < toolsTextViews.length; i++) {
            toolsTextViews[i].setSelected(false);
        }
        toolsTextViews[tag].setSelected(true);
    }

    @Override
    public void onBackPressedSupport() {
        super.onBackPressedSupport();
        finish();
    }


    public void getNetData(int secondType, boolean isFristInit, boolean isRefrsh, boolean isShowDialog, boolean isCancel, View emptyView, View onNetView, View errorView) {
        HashMap<String, String> parms = new HashMap<>();
        if (secondType == 0) {//点击全部的请求
            parms.put("pageIndex", String.valueOf(currentPage));
            parms.put("categoryId", secondIds.get(0));
        } else {//点击别的条目的请求
            String s = secondIds.get(flag);
            parms.put("pageIndex", String.valueOf(currentPage));
            parms.put("subCategoryId",s);
        }
        RetrofitFactory.getInstant().API()
                .getCateGoryList(parms)
                .compose(this.<CategoryListBean>setThread())
                .subscribe(new GjCustomStateViewObserver<CategoryListBean>(this, statlayout,
                        refrsh, isFristInit, isRefrsh, isShowDialog, isCancel, emptyView, onNetView, errorView) {
                    @Override
                    protected void onBusinessSuccess(CategoryListBean categoryListBean) throws Exception {
                        if (categoryListBean != null) {
                            List<CategoryListBean.DataBean.ItemListBean> itemList = categoryListBean.getData().getItemList();
                            if(currentPage ==1) {
                                cateGoryGoodListAdapter.setNewData(itemList);
                            }else {
                                cateGoryGoodListAdapter.addData(itemList);
                            }
                            currentPage++;
                        }
                        int itemCount = cateGoryGoodListAdapter.getData().size();
                        if(categoryListBean.getData().getTotalRecord() == itemCount){//数据加载完成
                            cateGoryGoodListAdapter.loadMoreEnd(false);

                        }else {
                            cateGoryGoodListAdapter.loadMoreComplete();
                        }

                    }

                    @Override
                    protected void onChildFailure(Throwable e, boolean isNetWorkError) throws Exception {

                    }


                });
    }
    @OnClick(R.id.search_et)
    public void search(View view){
        Intent intent = new Intent(this,SearchActivity.class);
        startActivity(intent);
    }

}
